package com.buyouchina.web.action.privilege;

import java.io.Serializable;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.buyouchina.bean.privilege.PrivilegeGroup;
import com.buyouchina.bean.privilege.SystemPrivilege;
import com.buyouchina.bean.privilege.SystemPrivilegePK;
import com.buyouchina.service.privilege.PrivilegeGroupService;
import com.buyouchina.service.privilege.SystemPrivilegeService;
import com.buyouchina.utils.SiteUrl;
import com.buyouchina.web.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
@Controller
public class PrivilegeGroupManageAction extends BaseAction implements ModelDriven<PrivilegeGroup>{
	private static final long serialVersionUID = 6238991178165254399L;
	private PrivilegeGroup privilegeGroup;
	private Integer groupid;
	private String name;
	private SystemPrivilegePK[] privileges;
	@Resource private PrivilegeGroupService privilegeGroupService;
	@Resource SystemPrivilegeService privilegeService;
	

	//	添加权限组界面
	@Permission(model="privilegeGroup",privilegeValue="insert")
	public String add(){
		request.setAttribute("privileges", privilegeService.getScrollData().getResultlist());
		return "add";
	}
	
	public String save(){
		if(name != null && !"".equals(name.trim())){	
			PrivilegeGroup group = new PrivilegeGroup();
			group.setName(name);
			for(SystemPrivilegePK id : privileges){
				group.addPrivilege(new SystemPrivilege(id));
			}
			privilegeGroupService.save(group);	
			request.setAttribute("message", "权限组添加成功");
			request.setAttribute("urladdress", SiteUrl.readUrl("privilegegroup.list"));
			return "message";
		}
		return "add";
	}
	//	修改
	@Permission(model="privilegeGroup",privilegeValue="update")
	public String edit(){
		privilegeGroup = privilegeGroupService.find(groupid);
		request.setAttribute("privileges", privilegeService.getScrollData().getResultlist());
		request.setAttribute("selectprivileges", privilegeGroup.getPrivileges());
		return "edit";		
	}
	public String update(){
		privilegeGroup = privilegeGroupService.find(groupid);
		if(name != null && !"".equals(name.trim())){
			privilegeGroup.setName(name);
			privilegeGroup.getPrivileges().clear();
			for(SystemPrivilegePK id : privileges){
				privilegeGroup.addPrivilege(new SystemPrivilege(id));
			}
			privilegeGroupService.update(privilegeGroup);	
			request.setAttribute("message", "权限组修改成功");
			request.setAttribute("urladdress", SiteUrl.readUrl("privilegegroup.list"));
			return "message";
		}	
		return "edit";
	}
	@Permission(model="privilegeGroup",privilegeValue="delete")
	public String delete(){
		privilegeGroupService.delete((Serializable)groupid);
		request.setAttribute("message", "权限组删除成功");
		request.setAttribute("urladdress", SiteUrl.readUrl("privilegegroup.list"));
		return "message";
	}
	

	public Integer getGroupid() {
		return groupid;
	}

	public void setGroupid(Integer groupid) {
		this.groupid = groupid;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public SystemPrivilegePK[] getPrivileges() {
		return privileges;
	}

	public void setPrivileges(SystemPrivilegePK[] privileges) {
		this.privileges = privileges;
	}

	public PrivilegeGroup getModel() {
		return privilegeGroup;
	}
}
